<?php

namespace app\admin\model\group;

use app\admin\model\user\UserModel;
use app\logic\model\group\GroupGoodsLogicModel;
use app\logic\model\group\GroupOrderLogicModel;
use app\logic\model\user\UserLogicModel;

class GroupOrderModel extends GroupOrderLogicModel
{

    public function getList(\think\Request $request, array $where, string $field='*', $page = 1, $limit=15,$order='id desc')
    {
        $query=$this->where($where)
            ->where(function ($q) use ($request){
                if(check_request($request,'buyer')){
                    $user_id_arr = UserModel::getIdsByAggregate($request['buyer']);
                    $q->whereIn("buyer_id",$user_id_arr);
                }
                if(check_request($request,'seller')){
                    $user_id_arr = UserModel::getIdsByAggregate($request['seller']);
                    $q->whereIn("sell_id",$user_id_arr);
                }
                if(check_request($request,'start_time')&&check_request($request,'end_time')){
                    $q->whereBetween("create_time",[$request['start_time'],$request['end_time']." 23:59:59"]);
                }elseif (check_request($request,'start_time')){
                    $q->where("create_time",">=",$request['start_time']);
                }elseif (check_request($request,'end_time')){
                    $q->where("create_time","<=",$request['end_time']." 23:59:59");
                }
            });
        $list = $query->field($field)->order($order)->page($page,$limit)->select();
        $count = $query->count();
        if($count >=1 ){
            foreach ($list as &$val){
                if($val['sell_id'] == 0){
                    $val['sell']=[];
                }else{
                    $val['sell'] = UserLogicModel::getInfoById($val['sell_id']);
                }
                $val['buyer'] = UserLogicModel::getInfoById($val['buyer_id']);
                $val['goods'] = GroupGoodsModel::where('id',$val['goods_id'])->find();
            }
            unset($val);
        }
        return [$list,$count];
    }

    public function getDetail($id){

        $info = $this->getFindById($id);
        if($info['sell_id'] == 0){
            $info['sell']=[];
        }else{
            $info['sell'] = UserLogicModel::getInfoById($info['sell_id']);
        }
        $info['buyer'] = UserLogicModel::getInfoById($info['buyer_id']);
        $info['goods'] = GroupGoodsModel::where('id',$info['goods_id'])->find();
        return $info;
    }
}